package adapteurMediaStore;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.insta.playdroid.metier.domain.Playlist;

public class AdapteurPlaylistMediaStore implements
		IAdapteurMediaStore<Playlist> {

	private Context context;

	public AdapteurPlaylistMediaStore(Context context) {
		this.context = context;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see adapteurMediaStore.IAdapteurMediaStore#collectAllValuesFromDB()
	 */
	@Override
	public List<Playlist> collectAllValuesFromDB() {

		List<Playlist> playlists = new ArrayList<Playlist>();

		String[] proj = { MediaStore.Audio.Playlists._ID,
				MediaStore.Audio.Playlists.NAME,
				MediaStore.Audio.Playlists.DATA };
		Uri tempPlaylistURI = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;

		// In the next line 'this' points to current Activity.
		// If you want to use the same code in other java file then activity,
		// then use an instance of any activity in place of 'this'.

		Cursor playListCursor = context.getContentResolver().query(
				tempPlaylistURI, proj, null, null, null);

		if (playListCursor == null) {
			Log.d("adap playlist",
					"Not having any Playlist on phone --------------");
			// new playlist
		}

		// System.gc();

		String playListName = null;
		int playListId;
		String playListPath = null;

		for (int i = 0; i < playListCursor.getCount(); i++) {
			playListCursor.moveToPosition(i);
			playListName = playListCursor.getString(playListCursor
					.getColumnIndex("name"));

			playListId = playListCursor.getInt(0);

			playListPath = playListCursor.getString(2);
			Playlist p = new Playlist(playListId, playListName, playListPath);
			playlists.add(p);
		}

		if (playListCursor != null)
			playListCursor.close();

		return playlists;
	}
	
	// public void onCLickAddPlaylist(View v) {
	// ArrayList<String> paths = new ArrayList<String>();
	// paths.add("/mnt/sdcard/media/Sound/Africa.mp3");
	// writePlaylist(this, "newPlaylist", paths);
	// }
	//
	// public static void writePlaylist(Context context, String playlistName,
	// ArrayList<String> paths) {
	// ContentResolver resolver = context.getContentResolver();
	// int playlistId = idForplaylist(context, playlistName);
	// Uri uri;
	// if (playlistId == -1) {
	// ContentValues values = new ContentValues(1);
	// values.put(MediaStore.Audio.Playlists.NAME, playlistName);
	// uri = resolver.insert(
	// MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);
	// playlistId = idForplaylist(context, playlistName);
	// } else {
	// uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
	// playlistId);
	// }
	// // Log.d(TAG, String.format("Writing playlist %s", uri));
	//
	// // Delete everything from the old playlist
	// context.getContentResolver().delete(uri, null, null);
	//
	// // Add all the new tracks to the playlist.
	// int size = paths.size();
	// ContentValues values[] = new ContentValues[size];
	// for (int k = 0; k < size; ++k) {
	// values[k] = new ContentValues();
	// values[k].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, k);
	// values[k].put(MediaStore.Audio.Playlists.Members.AUDIO_ID,
	// idFortrack(context, paths.get(k)));
	// }
	//
	// resolver.bulkInsert(uri, values);
	// }
	//
	// public static int idForplaylist(Context context, String name) {
	// Cursor c = query(context,
	// MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
	// new String[] { MediaStore.Audio.Playlists._ID },
	// MediaStore.Audio.Playlists.NAME + "=?", new String[] { name },
	// MediaStore.Audio.Playlists.NAME);
	// return intFromCursor(c);
	// }
	//
	// public static int idFortrack(Context context, String path) {
	// Cursor c = query(context,
	// MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
	// new String[] { MediaStore.Audio.Media._ID },
	// MediaStore.Audio.Media.DATA + "=?", new String[] { path },
	// MediaStore.Audio.Media.DATA);
	// return intFromCursor(c);
	// }
	//
	// private static int intFromCursor(Cursor c) {
	// int id = -1;
	// if (c != null) {
	// c.moveToFirst();
	// if (!c.isAfterLast()) {
	// id = c.getInt(0);
	// }
	// }
	// c.close();
	// return id;
	// }
	//
	// public static Cursor query(Context context, Uri uri, String[] projection,
	// String selection, String[] selectionArgs, String sortOrder,
	// int limit) {
	// try {
	// ContentResolver resolver = context.getContentResolver();
	// if (resolver == null) {
	// return null;
	// }
	// if (limit > 0) {
	// uri = uri.buildUpon().appendQueryParameter("limit", "" + limit)
	// .build();
	// }
	// return resolver.query(uri, projection, selection, selectionArgs,
	// sortOrder);
	// } catch (UnsupportedOperationException ex) {
	// return null;
	// }
	// }
	//
	// public static Cursor query(Context context, Uri uri, String[] projection,
	// String selection, String[] selectionArgs, String sortOrder) {
	// return query(context, uri, projection, selection, selectionArgs,
	// sortOrder, 0);
	// }
}
